package com.creatorblue.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.creatorblue.bean.UserInfo;
import com.creatorblue.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 用户信息控制层类
 * @author zhuq
 *
 */
@Controller
@RequestMapping("userinfo")
public class UserInfoController {

	Logger log = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 文件上传的名单
	 */
	List<String> writeList = Arrays.asList(".PNG", ".JPG", ".GIF");
	
	@Autowired
	private UserInfoService uis;
	@Value("${uploadPath}")
	private String path;
	
	/**
	 * 打开列表界面
	 * @param map 查询条件
	 * @param pageNum 当前第几页
	 * @param mav
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView queryAll(@RequestParam Map<String, Object> map, @RequestParam(defaultValue = "1")Integer pageNum, ModelAndView mav) {
		PageHelper.startPage(pageNum, 3);
		List<UserInfo> users = uis.queryAll(map);
		log.debug(users.toString());
		mav.setViewName("user/user_list");
		PageInfo<UserInfo> pageInfo = new PageInfo<>(users);
		mav.addObject("datas", users);
		mav.addObject("params", map);
		mav.addObject("pageInfo", pageInfo);
		return mav;
	}
	
	/**
	 * 打开新增/修改界面
	 * @param id 待修改的用户主键ID（新增时不传）
	 * @param mav
	 * @return
	 */
	@RequestMapping("toAdd")
	@PreAuthorize("hasAuthority('userinfo:add')")
	public ModelAndView toAdd(Integer id, ModelAndView mav) {
		mav.setViewName("user/user_add");
		if(id != null) {
			UserInfo user = uis.queryById(id);
			mav.addObject("user", user);
		}
		return mav;
	}
	
	/**
	 * 新增/修改处理方法
	 * @param user 待新增/修改的用户对象
	 * @param img 用户头像
	 * @param mav
	 * @return
	 */
	@RequestMapping("save")
	public ModelAndView save(UserInfo user, MultipartFile img, ModelAndView mav) {
		if(!img.isEmpty()) {
			String fileName = img.getOriginalFilename();
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			if(writeList.contains(suffix.toUpperCase())) {
				String fName = UUID.randomUUID().toString() + suffix;
				try {
					img.transferTo(new File(path + fName));
				} catch (Exception e) {
					e.printStackTrace();
				}
				user.setUserHeadimg(fName);
			}
		}
		if(user.getId() == null) {
			uis.insert(user);
		}else {
			uis.update(user);
		}
		mav.setViewName("redirect:/userinfo/list");
		return mav;
	}

	/**
	 * 删除方法
	 * @param ids 待删除的主键ID数组
	 * @param mav
	 * @return
	 */
	@RequestMapping("delete")
	@PreAuthorize("hasAuthority('userinfo:delete')")
	public ModelAndView delete(Integer[] ids, ModelAndView mav) {
		uis.delete(ids);
		
		mav.setViewName("redirect:/userinfo/list");
		return mav;
	}
	
}
